package com.ruoyi.estate.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.estate.domain.BuildingUnit;
import com.ruoyi.estate.domain.FloorTreeSelect;
import com.ruoyi.estate.mapper.BuildingUnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.estate.mapper.BusinessFloorMapper;
import com.ruoyi.estate.domain.BusinessFloor;
import com.ruoyi.estate.service.IBusinessFloorService;

/**
 * 楼栋管理Service业务层处理
 * 
 * @author 博雅
 * @date 2023-12-19
 */
@Service
public class BusinessFloorServiceImpl implements IBusinessFloorService 
{
    @Autowired
    private BusinessFloorMapper businessFloorMapper;
    @Autowired
    private BuildingUnitMapper  buildingUnitMapper;

    /**
     * 查询楼栋管理
     * 
     * @param floorId 楼栋管理主键
     * @return 楼栋管理
     */
    @Override
    public BusinessFloor selectBusinessFloorByFloorId(Long floorId)
    {
        return businessFloorMapper.selectBusinessFloorByFloorId(floorId);
    }

    /**
     * 查询楼栋管理列表
     * 
     * @param businessFloor 楼栋管理
     * @return 楼栋管理
     */
    @Override
    public List<BusinessFloor> selectBusinessFloorList(BusinessFloor businessFloor)
    {
        return businessFloorMapper.selectBusinessFloorList(businessFloor);
    }

    /**
     * 新增楼栋管理
     * 
     * @param businessFloor 楼栋管理
     * @return 结果
     */
    @Override
    public int insertBusinessFloor(BusinessFloor businessFloor)
    {
        businessFloor.setCreateTime(DateUtils.getNowDate());
        return businessFloorMapper.insertBusinessFloor(businessFloor);
    }

    /**
     * 修改楼栋管理
     * 
     * @param businessFloor 楼栋管理
     * @return 结果
     */
    @Override
    public int updateBusinessFloor(BusinessFloor businessFloor)
    {
        return businessFloorMapper.updateBusinessFloor(businessFloor);
    }

    /**
     * 批量删除楼栋管理
     * 
     * @param floorIds 需要删除的楼栋管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessFloorByFloorIds(Long[] floorIds)
    {
        return businessFloorMapper.deleteBusinessFloorByFloorIds(floorIds);
    }

    /**
     * 删除楼栋管理信息
     * 
     * @param floorId 楼栋管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessFloorByFloorId(Long floorId)
    {
        return businessFloorMapper.deleteBusinessFloorByFloorId(floorId);
    }

    @Override
    public List<FloorTreeSelect> selectFloorTreeList() {
        //查所有楼栋
        List<BusinessFloor> floors = this.selectBusinessFloorList(null);
        return buildDeptTreeSelect(floors);
    }

    public List<FloorTreeSelect> buildDeptTreeSelect(List<BusinessFloor> floors)
    {
        //查询楼栋的下级单元数据
        List<BusinessFloor> floorTrees = buildFloorTree(floors);
        //创建树节点集合
        List<FloorTreeSelect> trees=new ArrayList<>();
        //遍历楼栋信息
        for (BusinessFloor floor:floorTrees){
            //把楼栋信息，放入树节点对象
            FloorTreeSelect floorTree=new FloorTreeSelect(floor);
            //遍历楼栋下的单元信息
            for (BuildingUnit unit:floor.getChildren()){
                //把单元信息，放入树节点
                FloorTreeSelect unitTree=new FloorTreeSelect(unit);
                //把单元树节点，添加到楼栋节点下
                floorTree.getChildren().add(unitTree);
            }
            //把楼栋树节点，添加树节点集合
            trees.add(floorTree);
        }
        return trees;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param floors 部门列表
     * @return 树结构列表
     */
    public List<BusinessFloor> buildFloorTree(List<BusinessFloor> floors)
    {
        List<BusinessFloor> returnList = new ArrayList<BusinessFloor>();
        for(BusinessFloor floor:floors){
            Long floorId=floor.getFloorId();
            //查询单元信息
            List<BuildingUnit> list=buildingUnitMapper.selectBuildingUnitByFloorId(floorId);
            floor.setChildren(list);

            returnList.add(floor);
        }
        return returnList;
    }
}
